package com.trainbook.utils;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @Author: Dempsey
 * @Date: 2020/11/30 11:09
 * @Description:
 */
public class LocalDateTimeUtil {

    /**
     * LocalDateTime获取秒
     *
     * @param localDateTime
     * @return
     */
    public static Long LocalDateTimeConvertToSecond(LocalDateTime localDateTime) {
        return localDateTime.toEpochSecond(ZoneOffset.of("+8"));
    }

    /**
     * 秒转LocalDateTime
     *
     * @param second
     * @return
     */
    public static LocalDateTime secondConvertToLocalDateTime(Long second) {
        return LocalDateTime.ofEpochSecond(second, 0, ZoneOffset.ofHours(8));
    }

    /**
     * 毫秒转LocalDateTime
     *
     * @param milliSecond
     * @return
     */
    public static LocalDateTime milliSecondConvertToLocalDateTime(Long milliSecond) {
        return LocalDateTime.ofEpochSecond(milliSecond / 1000, 0, ZoneOffset.ofHours(8));
    }

    /**
     * 将java.util.Date 转换为java8 的java.time.LocalDateTime,默认时区为东8区
     *
     * @param date
     * @return
     */
    public static LocalDateTime dateConvertToLocalDateTime(Date date) {
        return date.toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
    }

    /**
     * 将java8 的 java.time.LocalDateTime 转换为 java.util.Date，默认时区为东8区
     *
     * @param localDateTime
     * @return
     */
    public static Date localDateTimeConvertToDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.toInstant(ZoneOffset.of("+8")));
    }

    /**
     * 时间格式化
     *
     * @param localDateTime
     * @param format
     * @return
     */
    public static String formatLocalDateTime(LocalDateTime localDateTime, String format) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        return localDateTime.format(formatter);
    }

    /**
     * 时间字符串转LocalDateTime
     *
     * @param dateTimeStr
     * @param format
     * @return
     */
    public static LocalDateTime reFormatLocalDateTime(String dateTimeStr, String format) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        return LocalDateTime.parse(dateTimeStr, formatter);
    }

    /**
     * 两个时间的 时间间隔 转化为 Long 类型
     * @param startTime
     * @param endTime
     * @return
     */
    public static Long countDuration(LocalDateTime startTime, LocalDateTime endTime) {
        return LocalDateTimeUtil.LocalDateTimeConvertToSecond(endTime) - LocalDateTimeUtil.LocalDateTimeConvertToSecond(startTime);
    }

    /**
     * 判断一个时间是否在另外两个时间之间
     * 在项目中，[前闭后闭]
     * @param time
     * @param startTime
     * @param endTime
     * @return
     */
    public static boolean ifin(LocalDateTime time ,LocalDateTime startTime, LocalDateTime endTime){
        if (time.isEqual(startTime) || time.isEqual(endTime)){
            return true;
        }
        if (time.isAfter(startTime) && time.isBefore(endTime)){
            return true;
        }
        return false;
    }

}
